package com.charger.cooperation.controller;

import java.text.ParseException;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.charger.common.utils.DateUtils;
import com.charger.cooperation.util.BeforeDateList;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.charger.common.annotation.Log;
import com.charger.common.core.controller.BaseController;
import com.charger.common.core.domain.AjaxResult;
import com.charger.common.enums.BusinessType;
import com.charger.cooperation.domain.ChargerOperation1;
import com.charger.cooperation.service.IChargerOperationService1;
import com.charger.common.utils.poi.ExcelUtil;

/**
 * 统计每日充电信息Controller
 * 
 * @author Qinkunpeng
 * @date 2024-11-27
 */
@RestController
@RequestMapping("/operation/operation1")
public class ChargerOperationController1 extends BaseController
{
    @Autowired
    private IChargerOperationService1 chargerOperationService1;

    /**
     * 查询统计每日充电信息列表
     */
    @GetMapping("/list")
    public Object list(ChargerOperation1 chargerOperation) throws ParseException {
//        startPage();

        Object o = null;
//        if (chargerOperation.getParams() != null) o = chargerOperation.getParams().get("endCreateTime");
        Date endTime = new Date();
        Date startTime = null;

//            endTime = list.get(0).getCreateTime();
//            startTime = list.get(list.size() - 1).getCreateTime();

        List<String> before = new ArrayList<>();
        List<Double> moneyData = new ArrayList<>();
        List<Double> chargeData = new ArrayList<>();
        List<Integer> personData= new ArrayList<>();

        before = BeforeDateList.getBefore(7, endTime);
        chargerOperation.getParams().put("beginCreateTime", before.get(before.size() - 1));
        chargerOperation.getParams().put("endCreateTime", before.get(0));
        List<ChargerOperation1> list = chargerOperationService1.selectChargerOperationList(chargerOperation);
        Map<String, ChargerOperation1> map = new HashMap<>();
        list.forEach(chargerOperation11 -> {
            map.put(DateUtils.parseDateToStr("yyyy-MM-dd",
                    chargerOperation11.getCreateTime()), chargerOperation11);
        });
        before.forEach(b -> {
//            System.out.println(b);
            ChargerOperation1 chargerOperation1 = map.get(b);
            if (chargerOperation1 != null) {
                moneyData.add(chargerOperation1.getChargeMoney());
                chargeData.add(chargerOperation1.getCharges());
                personData.add(Math.toIntExact(chargerOperation1.getCustomerNum()));

            }else{
                moneyData.add(0d);
                chargeData.add(0d);
                personData.add(0);
            }
        });
        Map<String, Object> result = new HashMap<>();
        result.put("cdate", before);
        result.put("moneyData", moneyData);
        result.put("chargeData", chargeData);
        result.put("personData", personData);
        result.put("page", new PageInfo<ChargerOperation1>(list));
        return AjaxResult.success(result);
    }


    /**
     * 导出统计每日充电信息列表
     */
    @PreAuthorize("@ss.hasPermi('operation:operation:export')")
    @Log(title = "统计每日充电信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChargerOperation1 chargerOperation1)
    {
        List<ChargerOperation1> list = chargerOperationService1.selectChargerOperationList(chargerOperation1);
        ExcelUtil<ChargerOperation1> util = new ExcelUtil<ChargerOperation1>(ChargerOperation1.class);
        util.exportExcel(response, list, "统计每日充电信息数据");
    }

    /**
     * 获取统计每日充电信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('operation:operation:query')")
    @GetMapping(value = "/{opId}")
    public AjaxResult getInfo(@PathVariable("opId") Long opId)
    {
        return success(chargerOperationService1.selectChargerOperationByOpId(opId));
    }

    /**
     * 新增统计每日充电信息
     */
    @PreAuthorize("@ss.hasPermi('operation:operation:add')")
    @Log(title = "统计每日充电信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChargerOperation1 chargerOperation1)
    {
        return toAjax(chargerOperationService1.insertChargerOperation(chargerOperation1));
    }

    /**
     * 修改统计每日充电信息
     */
    @PreAuthorize("@ss.hasPermi('operation:operation:edit')")
    @Log(title = "统计每日充电信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChargerOperation1 chargerOperation1)
    {
        return toAjax(chargerOperationService1.updateChargerOperation(chargerOperation1));
    }

    /**
     * 删除统计每日充电信息
     */
    @PreAuthorize("@ss.hasPermi('operation:operation:remove')")
    @Log(title = "统计每日充电信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{opIds}")
    public AjaxResult remove(@PathVariable Long[] opIds)
    {
        return toAjax(chargerOperationService1.deleteChargerOperationByOpIds(opIds));
    }
}
